package com.kc.web.controller.business;

import com.kc.business.domain.BizScenic;
import com.kc.business.domain.vo.BizScenicVo;
import com.kc.business.service.IBizScenicService;
import com.kc.business.service.ImageService;
import com.kc.common.annotation.Log;
import com.kc.common.annotation.RepeatSubmit;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import com.kc.common.utils.StringUtils;
import com.kc.common.utils.bean.BeanUtils;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 景区
 *
 * @author zhangshuo
 */
@RestController
@RequestMapping("/business/scenic")
public class BizScenicController extends BaseController {
    @Autowired
    private IBizScenicService bizScenicService;
    @Resource
    private ImageService imageService;
    public static final Integer DELETE = 1;

    /**
     * 获取景区列表
     */
    @PreAuthorize("@ss.hasPermi('business:scenic:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizScenic bizScenic) {
        startPage();
        List<BizScenic> list = bizScenicService.getList(bizScenic);
        for (BizScenic scenic : list) {
            //景区图片
            if (StringUtils.isNotBlank(scenic.getScenicImg())) {
                String[] scenicImgs = StringUtils.split(scenic.getScenicImg(), ",");
                scenic.setScenicImgs(scenicImgs);
                scenic.setGoodsIdArray(stringToIntArray(scenic.getGoodsId()));
            }else{
                scenic.setScenicImgs(new String[0]);
            }
        }
        return getDataTable(list);
    }

    /**
     * 获取景区详情
     * @param scenicId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:scenic:info')")
    @GetMapping("/info/{scenicId}")
    public AjaxResult getInfoByUserId(@PathVariable Integer scenicId) {

        //List<BizScenic>  bizScenicList = bizScenicService.getList(bizScenic);
      // BizScenic bizScenic = bizScenicService.getInfoByUserId(userId);
        BizScenic  bizScenic = bizScenicService.getInfoById(scenicId);
        if(bizScenic.getScenicImgs() == null){
            String[] arr = new String[0];
            bizScenic.setScenicImgs(arr);
        }
        if(StringUtils.isNotEmpty(bizScenic.getScenicImg())){
            bizScenic.setScenicImgs(StringUtils.split(bizScenic.getScenicImg(),","));
        }
        return AjaxResult.success(bizScenic);
    }

    /**
     * 新增景区
     */
    @PreAuthorize("@ss.hasPermi('business:scenic:add')")
    @Log(title = "景区管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody BizScenic bizScenic) {
        //图片数组转字符串逗号分隔
        bizScenic.setScenicImg(StringUtils.join(bizScenic.getScenicImgs(), ","));
        //处理列表图片宽高
        bizScenic.setImageAttr(imageService.getImageSizeToJson(bizScenic.getScenicImgs()));
        bizScenic.setIsPersonalInfoPerfected(1);
        bizScenic.setGoodsId(integerArrayToString(bizScenic.getGoodsIdArray()));
        return toAjax(bizScenicService.add(bizScenic));
    }


    /**
     * 编辑景区
     */
    @PreAuthorize("@ss.hasPermi('business:scenic:edit')")
    @Log(title = "景区管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@Validated @RequestBody BizScenic bizScenic) {
        //图片数组转字符串逗号分隔
        bizScenic.setScenicImg(StringUtils.join(bizScenic.getScenicImgs(), ","));
        //处理列表图片宽高
        bizScenic.setImageAttr(imageService.getImageSizeToJson(bizScenic.getScenicImgs()));
        bizScenic.setGoodsId(integerArrayToString(bizScenic.getGoodsIdArray()));
        return toAjax(bizScenicService.edit(bizScenic));
    }

    /**
     * 删除景区
     */
    @PreAuthorize("@ss.hasPermi('business:scenic:remove')")
    @Log(title = "景区管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{scenicId}")
    public AjaxResult remove(@PathVariable Integer scenicId) {
        BizScenic bizScenic = new BizScenic();
        bizScenic.setId(scenicId);
        bizScenic.setIsDeleted(DELETE);
        return toAjax(bizScenicService.delete(bizScenic));
    }

    /**
     * 获取景区下拉列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect(BizScenic bizScenic) {
        List<BizScenic> list = bizScenicService.getList(bizScenic);
        List<BizScenicVo> voList = new ArrayList<>();
        for (BizScenic scenic : list) {
            BizScenicVo bizScenicVo = new BizScenicVo();
            BeanUtils.copyBeanProp(bizScenicVo, scenic);
            voList.add(bizScenicVo);
        }
        return AjaxResult.success(voList);
    }

    private Integer[] stringToIntArray(String str) {
        if (StringUtils.isNotEmpty(str)) {
            String[] arr = str.split(",");
            Integer[] ints = new Integer[arr.length];
            for (int i = 0; i < arr.length; i++) {
                ints[i] = Integer.parseInt(arr[i]);
            }
            return ints;
        }
        return new Integer[0];
    }

    private String integerArrayToString(Integer[] arr) {
        if (ArrayUtils.isNotEmpty(arr)) {
            StringBuilder sb = new StringBuilder();
            for (Integer item : arr) {
                sb.append(item);
                sb.append(",");
            }
            String result = sb.toString();
            if (result.length() > 0) {
                result = result.substring(0, result.length() - 1);
            }
            return result;
        }
        return null;
    }

}
